#!/bin/sh

BASE_PATH=$(
  cd $(dirname "$0");
  pwd
)

PDFM_DISP_CRACK="${BASE_PATH}/prl_disp_service"
PDFM_DISP_DST="/Applications/Parallels Desktop.app/Contents/MacOS/Parallels Service.app/Contents/MacOS/prl_disp_service"
PDFM_DISP_ENT="${BASE_PATH}/ParallelsService.entitlements"

LICENSE_FILE="${BASE_PATH}/licenses.json"
LICENSE_DST="/Library/Preferences/Parallels/licenses.json"

echo "[√] Kill Parallels Desktop"

killall -9 prl_client_app 2>/dev/null
sudo pkill -9 prl_disp_service 2>/dev/null

echo "[√] Copy prl_disp_service"

sudo cp -f "${PDFM_DISP_CRACK}" "${PDFM_DISP_DST}"
sudo chown root:wheel "${PDFM_DISP_DST}"
sudo chmod 755 "${PDFM_DISP_DST}"

echo "[√] Sign prl_disp_service"

sudo codesign -f -s - --timestamp=none --all-architectures --entitlements "${PDFM_DISP_ENT}" "${PDFM_DISP_DST}"

echo "[√] Copy licenses.json"

sudo rm -f "${LICENSE_DST}" > /dev/null
sudo cp "${LICENSE_FILE}" "${LICENSE_DST}"
sudo chown root:wheel "${LICENSE_DST}"
sudo chmod 444 "${LICENSE_DST}"

echo "[√] Crack over"

sudo cat >> /etc/hosts <<'EOF'

# isolate for Parallels
127.0.0.1 download.parallels.com
127.0.0.1 update.parallels.com
127.0.0.1 desktop.parallels.com
127.0.0.1 download.parallels.com.cdn.cloudflare.net
127.0.0.1 update.parallels.com.cdn.cloudflare.net
127.0.0.1 desktop.parallels.com.cdn.cloudflare.net
127.0.0.1 www.parallels.cn
127.0.0.1 www.parallels.com
127.0.0.1 reportus.parallels.com
127.0.0.1 parallels.com
127.0.0.1 parallels.cn
127.0.0.1 pax-manager.myparallels.com
127.0.0.1 myparallels.com
127.0.0.1 my.parallels.com

EOF

echo "[×] Host local DNS: Always Failed, you should do that as User 0 Manually"